package cn.wolfcode.controller;

import cn.wolfcode.domain.SysDictData;
import cn.wolfcode.domain.SysDictType;
import cn.wolfcode.service.ISysDictDataService;
import cn.wolfcode.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by XuHangBin
 */
@Controller
@RequestMapping("/sysDictData")
public class SysDictDataController {

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    @RequestMapping("/list")
    public String list(String dict_type, Model model) {
        model.addAttribute("sysDictDataList", sysDictDataService.queryByType(dict_type));
        model.addAttribute("type", sysDictTypeService.queryByType(dict_type));
        return "sysDictData/view";
    }


    @RequestMapping("/add")
    public String add(String dict_type, Model model) {
        SysDictType sysDictType = sysDictTypeService.queryByType(dict_type);
        model.addAttribute("sysDictType", sysDictType);
        return "sysDictData/add";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SysDictData sysDictData, Model model) {
        sysDictDataService.saveOrUpdate(sysDictData);

        String dict_type = sysDictData.getDict_type();
        System.out.println("dict_type = " + dict_type);

        return "redirect:/sysDictData/list?dict_type=" + dict_type;
    }

    @RequestMapping("/edit")
    public String edit(Long id, Model model) {
        //根据id查询数据指定明细对象,放入到作用域中
        System.out.println("id = " + id);
        SysDictData sysDictData = sysDictDataService.get(id);
        model.addAttribute("sysDictData", sysDictData);

        //根据数据字典明细的dictType查询数据字典对象,放入到作用域中
        SysDictType sysDictType = sysDictTypeService.getByDictType(sysDictData.getDict_type());
        model.addAttribute("sysDictType", sysDictType);
        return "sysDictData/edit";
    }


}
